package com.seed.util.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Java在DOS下执行Oracle命令进行数据导入导出
 * @author yanghaoquan
 *
 */
public class SyncExpImpUtil {
	private static Log log = LogFactory.getLog(SyncExpImpUtil.class);
	public static void main(String[] args) {
		exp("sys", "yhqmcq", "seed", "seed_org", "d:/dd.dmp", null) ;
	}
	/**
	 * 导出
	 * @param userName  数据库用户名
	 * @param passWord  数据库密码
	 * @param dataBaseName  数据库名
	 * @param tabName  表名
	 * @param address  导出地址
	 * @param where  数据过滤条件
	 */
	public static boolean exp(String userName,String passWord,String dataBaseName,String tabName,String address,String where){
		boolean bool = false;
		// 拼装DOS命令进行数据库备份
		StringBuffer exp = new StringBuffer("exp ");
		exp.append(userName);
		exp.append("/");
		exp.append(passWord);
		exp.append("@");
		exp.append(dataBaseName);
		exp.append(" TRIGGERS=N ");
		if(null != tabName && !"".equals(tabName)){
			exp.append(" tables=");
			exp.append(tabName);
		}
		exp.append(" file=");
		exp.append(address);
		if(null != tabName && !"".equals(where)){
			exp.append(" query=\\\"where ");
			exp.append(where);
			exp.append(" \\\" ");
		}
		try {
			log.info(exp.toString());
			//System.out.println(exp.toString());
			Process p = Runtime.getRuntime().exec(exp.toString());
			InputStreamReader isr = new InputStreamReader(p.getErrorStream()); 
			BufferedReader br = new BufferedReader(isr);
			String line = null;   
			while ((line = br.readLine()) != null){   
				if(line.indexOf("错误")!=-1){    
				break;   
				}  
			}
			p.destroy();
			p.waitFor();
			bool = true;
		} catch (Exception e) {
			log.error("导出文件"+tabName+"失败");
			e.printStackTrace();
		} 
		return bool;
	}
	/**
	 * 导入
	 * @param userName  数据库用户名
	 * @param passWord  数据库密码
	 * @param dataBaseName  数据库名
	 * @param tabName  表名
	 * @param address  导出地址
	 * @param where  数据过滤条件
	 * @param ignore true追加(数据导出模式)
	 */
	public static boolean imp(String userName,String passWord,String dataBaseName,String tabName,String address,boolean ignore){
		boolean bool = false;
		// 拼装DOS命令进行数据库备份
		StringBuffer imp = new StringBuffer("imp ");
		imp.append(userName);
		imp.append("/");
		imp.append(passWord);
		imp.append("@");
		imp.append(dataBaseName);
		if(ignore){
			imp.append(" ignore=y");
		}
		if(null != tabName && !"".equals(tabName)){
			imp.append(" tables=");
			imp.append(tabName);
		}
		imp.append(" file=");
		/*
		* 得到存储地址的最后一个字符 如果有\就直接拼装地址 如果没有\就加上/然后拼装数据库名称
		*/
		imp.append(address);
		try {
			log.info(imp.toString());
			//System.out.println(imp.toString());
			Process p = Runtime.getRuntime().exec(imp.toString());
			InputStreamReader isr = new InputStreamReader(p.getErrorStream()); 
			BufferedReader br = new BufferedReader(isr);
			String line = null;   
			while ((line = br.readLine()) != null){   
				if(line.indexOf("错误")!=-1){    
				break;   
				}
			}
			p.destroy();
			p.waitFor();
			bool = true;
		} catch (Exception e) {
			log.error("导入文件"+tabName+"失败");
			e.printStackTrace();
		} 
		return bool;
	}

}
